package com.zoffy.wmsbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoffy.wmsbackend.common.RequestParam;
import com.zoffy.wmsbackend.model.dto.warehouse.WareHouseDTO;
import com.zoffy.wmsbackend.model.entity.Warehouse;
import com.zoffy.wmsbackend.model.vo.WarehouseVO;
import com.zoffy.wmsbackend.service.WarehouseService;
import com.zoffy.wmsbackend.mapper.WarehouseMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
* @author 86170
* @description 针对表【warehouse(仓库表)】的数据库操作Service实现
* @createDate 2025-01-02 01:34:40
*/
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse>
    implements WarehouseService{

    @Override
    public IPage<WarehouseVO> listWarehousePage(RequestParam<WareHouseDTO> requestParam) {
        Integer pageNum = requestParam.getPageNum();
        Long pageSize = requestParam.getPageSize();
        WareHouseDTO params = requestParam.getParams();
        String warehouseName = null;
        String address = null;
        String warehouseCode = null;
        if(params!= null){
            warehouseName = params.getWarehouseName();
            address = params.getAddress();
            warehouseCode = params.getWarehouseCode();
        }
        LambdaQueryWrapper<Warehouse> wrapper=Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(warehouseName),Warehouse::getWarehouseName,"%"+warehouseName+"%");
        wrapper.like(StringUtils.isNotBlank(address),Warehouse::getAddress,"%"+address+"%");
        wrapper.eq(StringUtils.isNotBlank(warehouseCode),Warehouse::getWarehouseCode,warehouseCode);

        Page<Warehouse> page=new Page<>(pageNum,pageSize);
        Page<Warehouse> warehousePage = this.page(page, wrapper);
        IPage<WarehouseVO> warehouseVOIPage = warehousePage.convert(WarehouseVO::obj2vo);
        return warehouseVOIPage;
    }

    @Override
    public Boolean addWarehouse(WareHouseDTO dto) {
        String warehouseName = dto.getWarehouseName();
        String address = dto.getAddress();
        String warehouseCode = dto.getWarehouseCode();
        if(warehouseCode == null){
            warehouseCode = UUID.randomUUID().toString().replace("-","");
        }
        Long manufacturerId = dto.getManufacturerId();
        Warehouse warehouse=new Warehouse();
        warehouse.setWarehouseName(warehouseName);
        warehouse.setWarehouseCode(warehouseCode);
        warehouse.setAddress(address);
        warehouse.setManufacturerId(manufacturerId);
        warehouse.setWarehouseCode(warehouseCode);
        return this.save(warehouse);
    }

    @Override
    public Boolean updateWarehouse(WareHouseDTO dto) {
        String warehouseName = dto.getWarehouseName();
        String address = dto.getAddress();
        Warehouse warehouse = this.getById(dto.getId());
        if(warehouse == null){
            return false;
        }
        warehouse.setAddress(address);
        warehouse.setWarehouseName(warehouseName);
        return this.updateById(warehouse);
    }

    @Override
    public Boolean deleteWarehouse(WareHouseDTO dto) {
        return this.removeById(dto.getId());
    }

}




